package com.example.wan_android.adapter

import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.example.wan_android.BR
import com.example.wan_android.R
import com.example.wan_android.core.CoreViewHolder
import com.example.wan_android.databinding.AdapterAuthorContentBinding
import com.example.wan_android.http.bean.AuthorDatumData
import com.example.wan_android.viewmodel.AuthorViewModel

class AuthorContentAdapter(vm: AuthorViewModel) :
    CoreAdapter<AdapterAuthorContentBinding, AuthorViewModel, AuthorDatumData>(
        BR.data,
        BR.viewModel,
        vm
    ) {
    override fun getItemLayoutId(viewType: Int): Int {
        return R.layout.adapter_author_content
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CoreViewHolder {
        val holder = super.onCreateViewHolder(parent, viewType)
        holder.binding.root.findViewById<ImageView>(R.id.iv_love).setOnClickListener {
            val position = holder.adapterPosition
            mViewModel?.unCollection(mListData[position].id)
            mListData[position].collect = false
            notifyItemChanged(position)
        }
        holder.binding.root.findViewById<ImageView>(R.id.iv_love).setOnClickListener {
            val position = holder.adapterPosition
            mViewModel?.collection(mListData[position].id)
            mListData[position].collect = true
            notifyItemChanged(position)
        }
        return holder
    }

    override fun onBindViewHolder(holder: CoreViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        holder.binding.root.findViewById<View>(R.id.iv_love).isSelected =
            mListData[position].collect
    }

}